home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / ui / scrollview.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  15.2 KB  |  418 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2007 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Local
  23. from base.g import *
  24. from prnt import cups
  25.  
  26. # Qt
  27. from qt import *
  28.  
  29. class Widget(QWidget):
  30.     def __init__(self, parent=None, name=None, fl=0):
  31.         QWidget.__init__(self, parent, name, fl)
  32.         self.control = None
  33.         
  34.     def setControl(self, control):
  35.         self.control = control
  36.         
  37.         
  38.  
  39. class ScrollView(QScrollView):
  40.     def __init__(self,parent = None,name = None,fl = 0):
  41.         QScrollView.__init__(self,parent,name,fl)
  42.         self.items = {}
  43.         self.enableClipper(True)
  44.         self.viewport().setPaletteBackgroundColor(qApp.palette().color(QPalette.Active, QColorGroup.Background))
  45.         self.cur_device = None
  46.         self.cur_printer = None
  47.         self.item_margin = 2
  48.         self.y = 0
  49.         self.printers = []
  50.         self.maximize = None
  51.         self.orig_height = 0
  52.         
  53.         if log.get_level() == log.LOG_LEVEL_DEBUG:
  54.             self.heading_color = qApp.palette().color(QPalette.Active, QColorGroup.Base)
  55.             self.frame_shape = QFrame.Box
  56.         else:
  57.             self.heading_color = qApp.palette().color(QPalette.Active, QColorGroup.Background)            
  58.             self.frame_shape = QFrame.NoFrame
  59.             
  60.     def getWidget(self):
  61.         widget = Widget(self.viewport(),"widget")
  62.         #widget.setPaletteBackgroundColor(qApp.palette().color(QPalette.Active, QColorGroup.Base))
  63.         widget.setPaletteBackgroundColor(qApp.palette().color(QPalette.Active, QColorGroup.Background))
  64.         widget.setSizePolicy(QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Maximum))
  65.         widget.resize(self.visibleWidth(), self.size().height())
  66.         widget.setMinimumWidth(self.visibleWidth())
  67.         widget.resize(self.viewport().size().width(), self.size().height())
  68.         return widget
  69.  
  70.     def viewportResizeEvent(self, e):
  71.         QScrollView.viewportResizeEvent(self, e)
  72.  
  73.         total_height = 0
  74.         item_margin = self.item_margin
  75.         width = e.size().width()
  76.         
  77.         for w in self.items:
  78.             height  = self.items[w].size().height()
  79.             self.items[w].resize(width, height)
  80.             self.items[w].setMinimumWidth(width)
  81.             total_height += (height + item_margin)
  82.             
  83.         if self.maximize is not None:
  84.             self.maximizeControl(total_height)
  85.         
  86.         self.resizeContents(e.size().width(), total_height)
  87.             
  88.     def maximizeControl(self, total_height=0):
  89.         if self.maximize is not None:
  90.             
  91.             if total_height == 0:
  92.                 item_margin = self.item_margin
  93.                 for w in self.items:
  94.                     total_height += (self.items[w].size().height() + item_margin)
  95.             
  96.             width = self.items[self.maximize].size().width()
  97.             old_height = self.items[self.maximize].size().height()
  98.             
  99.             new_height = max((self.visibleHeight()-(total_height-old_height)), 
  100.                 self.orig_height)
  101.                 
  102.             delta = new_height - old_height
  103.             
  104.             if delta:
  105.                 self.items[self.maximize].resize(width, new_height)
  106.                 self.resizeContents(width, self.contentsHeight()+delta)
  107.                 m_y = self.childY(self.items[self.maximize])
  108.                 
  109.                 for w in self.items:
  110.                     w_y = self.childY(self.items[w])
  111.                     if w_y > m_y:
  112.                         self.moveChild(self.items[w], 0, w_y+delta)
  113.     
  114.     def isFax(self):
  115.         self.is_fax = False
  116.         self.printers = cups.getPrinters()
  117.         for p in self.printers:
  118.             if p.name == self.cur_printer:
  119.                 if p.device_uri.startswith("hpfax:"):
  120.                     self.is_fax = True
  121.                 
  122.                 break
  123.         
  124.     def onDeviceChange(self, cur_device=None):
  125.         if cur_device is not None:
  126.             log.debug("onDeviceChange(%s)" % cur_device.device_uri)
  127.         else:
  128.             log.debug("onDeviceChange(None)")
  129.             
  130.         if cur_device is not None:
  131.             self.cur_device = cur_device
  132.  
  133.         if self.cur_device is not None and self.cur_device.supported:
  134.             try:
  135.                 self.cur_printer = self.cur_device.cups_printers[0]
  136.             except IndexError:
  137.                 log.error("Printer list empty")
  138.             else:
  139.                 self.isFax()
  140.                 
  141.                 QApplication.setOverrideCursor(QApplication.waitCursor)
  142.                 try:
  143.                     try:
  144.                         self.fillControls()
  145.                     except Exception, e:
  146.                         log.exception()
  147.                 finally:
  148.                     QApplication.restoreOverrideCursor()
  149.         
  150.         else:
  151.             log.debug("Unsupported device")
  152.             self.y = 0
  153.             self.clear()
  154.  
  155.     def fillControls(self):
  156.         log.debug("fillControls(%s)" % str(self.name()))
  157.         self.y = 0
  158.         self.clear()
  159.  
  160.     def onPrinterChange(self, printer_name):
  161.         if printer_name == self.cur_printer:
  162.             return
  163.         
  164.         self.cur_printer = str(printer_name)
  165.         
  166.         if self.cur_device is not None and self.cur_device.supported:
  167.             self.isFax()
  168.             QApplication.setOverrideCursor(QApplication.waitCursor)
  169.             try:
  170.                 try:
  171.                     self.fillControls()
  172.                 except Exception, e:
  173.                     log.exception()
  174.             finally:
  175.                 QApplication.restoreOverrideCursor()
  176.                 
  177.             try:
  178.                 self.printerComboBox.setCurrentText(self.cur_printer)
  179.             except AttributeError:
  180.                 pass
  181.         
  182.         else:
  183.             self.y = 0
  184.             self.clear()
  185.  
  186.     def addWidget(self, widget, key, control=None, maximize=False):
  187.         try:
  188.             self.items[key]
  189.         except KeyError:
  190.             if maximize:
  191.                 self.maximize = key
  192.                 widget.resize(widget.size().width(), 150)
  193.                 self.orig_height = widget.size().height()
  194.                 
  195.             widget.setControl(control)
  196.             self.items[key] = widget
  197.             widget.setMinimumWidth(self.visibleWidth())
  198.             widget.adjustSize()
  199.             self.addChild(widget, 0, self.y)
  200.             self.y += (widget.size().height() + self.item_margin)
  201.             self.resizeContents(self.visibleWidth(), self.y)
  202.             widget.show()
  203.         else:
  204.             log.debug("ERROR: Duplicate control name: %s" % key)
  205.  
  206.     def clear(self):
  207.         if len(self.items):
  208.             for x in self.items:
  209.                 self.removeChild(self.items[x])
  210.                 self.items[x].hide()
  211.  
  212.             self.items.clear()
  213.  
  214.     def addGroupHeading(self, group, heading, read_only=False):
  215.         widget = self.getWidget()
  216.         widget.setMinimumHeight(30)
  217.         widget.setMaximumHeight(30)
  218.         
  219.         if heading:
  220.             widget.setPaletteBackgroundColor(self.heading_color)
  221.         
  222.         layout = QGridLayout(widget, 1, 1, 5, 10, "layout")
  223.         textLabel2 = QLabel(widget, "textLabel2")
  224.         
  225.         if heading:
  226.             textLabel2.setFrameShape(QFrame.TabWidgetPanel)
  227.         
  228.         textLabel2.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, 
  229.             QSizePolicy.Minimum, 0, 0,
  230.             textLabel2.sizePolicy().hasHeightForWidth()))
  231.         
  232.         textLabel2.setAlignment( QLabel.AlignLeft | QLabel.AlignVCenter)
  233.         layout.addWidget(textLabel2, 0, 0)
  234.  
  235.         if read_only:
  236.             textLabel2.setText(self.__tr("<b>%1 (read only)</b>").arg(heading))
  237.         else:
  238.             textLabel2.setText(QString("<b>%1</b>").arg(heading))
  239.  
  240.         self.addWidget(widget, "g:"+str(group))
  241.         
  242.         
  243.     def addActionButton(self, name, action_text, action_func, nav_text ='', nav_func=None):
  244.         widget = self.getWidget()
  245.         
  246.         widget.setPaletteBackgroundColor(qApp.palette().color(QPalette.Active, QColorGroup.Highlight))
  247.         self.actionPushButton = None
  248.         self.navPushButton = None
  249.         
  250.         layout36 = QHBoxLayout(widget,5,10,"layout36")
  251.  
  252.         if nav_func is not None:
  253.             self.navPushButton = QPushButton(widget,"navPushButton")
  254.             navPushButton_font = QFont(self.navPushButton.font())
  255.             navPushButton_font.setBold(1)
  256.             self.navPushButton.setFont(navPushButton_font)
  257.             self.navPushButton.setText(nav_text)
  258.             layout36.addWidget(self.navPushButton)
  259.             
  260.             self.connect(self.navPushButton, SIGNAL("clicked()"), nav_func)
  261.             
  262.         spacer35 = QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  263.         layout36.addItem(spacer35)
  264.  
  265.         if action_func is not None:
  266.             self.actionPushButton = QPushButton(widget,"actionPushButton")
  267.             actionPushButton_font = QFont(self.actionPushButton.font())
  268.             actionPushButton_font.setBold(1)
  269.             self.actionPushButton.setFont(actionPushButton_font)
  270.             layout36.addWidget(self.actionPushButton)
  271.             
  272.             self.actionPushButton.setText(action_text)
  273.         
  274.             self.connect(self.actionPushButton, SIGNAL("clicked()"), action_func)
  275.         
  276.         self.addWidget(widget, name)
  277.         
  278.         if self.actionPushButton is not None:
  279.             return self.actionPushButton
  280.         elif self.navPushButton is not None:
  281.             return self.navPushButton
  282.         else:
  283.             return None
  284.         
  285.     def printerComboBox_activated(self, p):
  286.         self.cur_printer = str(p)
  287.         
  288.     def addPrinterFaxList(self, printers=True, faxes=False):
  289.         widget = self.getWidget()
  290.         
  291.         layout = QGridLayout(widget,1,1,5,10,"layout")
  292.  
  293.         self.printernameTextLabel = QLabel(widget,"printernameTextLabel")
  294.         layout.addWidget(self.printernameTextLabel,0,0)
  295.  
  296.         self.printerComboBox = QComboBox(0,widget,"printerComboBox")
  297.         layout.addWidget(self.printerComboBox,0,1)
  298.         
  299.         if printers and faxes:
  300.             self.addGroupHeading("printer_list_heading", self.__tr("Printer/Fax"))
  301.             self.printernameTextLabel.setText(self.__tr("Printer/Fax Name:"))
  302.         elif printers:
  303.             self.addGroupHeading("printer_list_heading", self.__tr("Printer"))
  304.             self.printernameTextLabel.setText(self.__tr("Printer Name:"))
  305.         else:
  306.             self.addGroupHeading("printer_list_heading", self.__tr("Fax"))
  307.             self.printernameTextLabel.setText(self.__tr("Fax Name:"))
  308.             
  309.         for p in self.printers:
  310.             if p.device_uri == self.cur_device.device_uri or \
  311.                 p.device_uri == self.cur_device.device_uri.replace("hp:", "hpfax:"):
  312.                     if (p.device_uri.startswith("hpfax:") and faxes) or \
  313.                        (p.device_uri.startswith("hp:") and printers):
  314.                         
  315.                         self.printerComboBox.insertItem(p.name)
  316.                         
  317.         self.connect(self.printerComboBox, SIGNAL("activated(const QString&)"), self.printerComboBox_activated)
  318.         
  319.         self.addWidget(widget, "printer_list")
  320.         
  321.         
  322.     def addLoadPaper(self):
  323.         self.addGroupHeading("load_paper", self.__tr("Load Paper"))
  324.         
  325.         widget = self.getWidget()
  326.         layout1 = QGridLayout(widget, 1, 2, 5, 10,"layout1")
  327.         
  328.         layout1.setColStretch(0, 1)
  329.         layout1.setColStretch(1, 10)
  330.         
  331.         icon = QLabel(widget, "icon")
  332.         icon.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed, 0, 0,
  333.             icon.sizePolicy().hasHeightForWidth()))
  334.  
  335.         icon.setScaledContents(1)
  336.         layout1.addWidget(icon, 0, 0)
  337.  
  338.         textLabel = QLabel(widget, "textLabel")
  339.         textLabel.setAlignment(QLabel.WordBreak)
  340.         textLabel.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred, 0, 0,
  341.             textLabel.sizePolicy().hasHeightForWidth()))        
  342.         textLabel.setFrameShape(self.frame_shape)
  343.         layout1.addWidget(textLabel, 0, 1)
  344.  
  345.         spacer1 = QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
  346.         layout1.addItem(spacer1, 0, 2)
  347.  
  348.         textLabel.setText(self.__tr("A page will be printed. Please load <b>plain paper</b> into the printer."))
  349.         icon.setPixmap(QPixmap(os.path.join(prop.image_dir, "load_paper.png")))
  350.  
  351.         self.addWidget(widget, "load_paper")
  352.         
  353.         
  354.  
  355.     def __tr(self,s,c = None):
  356.         return qApp.translate("DevMgr4",s,c)
  357.         
  358.         
  359.         
  360. class PixmapLabelButton(QPushButton):
  361.     def __init__(self, parent=None, pixmap=None, disabled_pixmap=None, name=''):
  362.         QPushButton.__init__(self, parent, name)
  363.         
  364.         if type(pixmap) == type(''):
  365.             self.pixmap = QPixmap(os.path.join(prop.image_dir, pixmap))
  366.         else:
  367.             self.pixmap = pixmap
  368.         
  369.         if type(disabled_pixmap) == type(''):
  370.             self.disabled_pixmap = QPixmap(os.path.join(prop.image_dir, disabled_pixmap))
  371.         else:
  372.             self.disabled_pixmap = disabled_pixmap
  373.         
  374.         self.pixmap_width, self.pixmap_height = self.pixmap.width(), self.pixmap.height()
  375.         self.width_set = None
  376.  
  377.         
  378.     def drawButtonLabel(self, painter):
  379.         button_width, button_height = self.width(), self.height()
  380.         
  381.         adj = 0
  382.         if self.isDown():
  383.             adj = 1
  384.         
  385.         
  386.         if self.isEnabled():
  387.             painter.setPen(Qt.black)
  388.         else:
  389.             painter.setPen(Qt.gray)
  390.         
  391.         #f = QFont()
  392.         #painter.setFont(f)
  393.  
  394.         text_rect = painter.boundingRect(0, 0, 1000, 1000, Qt.AlignLeft, self.text())
  395.         text_width, text_height = text_rect.right() - text_rect.left(), text_rect.bottom() - text_rect.top()
  396.         
  397.         button_width_center = button_width/2
  398.         button_height_center = button_height/2
  399.         combined_width_center = (self.pixmap_width + text_width + 10)/2
  400.         
  401.         
  402.         if self.isEnabled():
  403.             painter.drawPixmap(button_width_center - combined_width_center + adj,
  404.                 button_height_center - self.pixmap_height/2 + adj, self.pixmap)
  405.         else:
  406.             painter.drawPixmap(button_width_center - combined_width_center + adj,
  407.                 button_height_center - self.pixmap_height/2 + adj, self.disabled_pixmap)
  408.         
  409.         if self.width_set is None:
  410.             self.setMinimumWidth(self.pixmap_width + text_width + 20)
  411.             self.width_set = 0
  412.         
  413.         painter.drawText(button_width_center - combined_width_center + 
  414.             self.pixmap_width + 5 + adj, 
  415.             button_height_center - text_height/2 + adj, 1000, 1000, 
  416.             Qt.AlignLeft, self.text())
  417.                                  
  418.